/**
 * Created by Administrator on 2018/6/9.
 */

function addDeptInit($scope, $route, CertusFormAjax, CertusJsonAjax, $location, $http,$routeParams) {
    $scope.dept = {};

    $scope.submitted = true;


    CertusFormAjax.ajax("companyList",{},function(res){
        $scope.companySelect = eval("("+res.data+")");
        if (!angular.isUndefined($routeParams.departmentCode) && "@@@@" != $routeParams.departmentCode) {
            CertusFormAjax.ajax("deptInfo", {departmentCode: $routeParams.departmentCode}, function (res) {
                $scope.dept = res.data;
                CertusFormAjax.ajax("deptList", {companyCode:res.data.companyCode}, function (res) {
                    $scope.departmentSelect =eval("("+res.data+")");
                    //先判断 是否 有 参数值 带过来

                })
            })
        }else{
            CertusFormAjax.ajax("deptList", {}, function (res) {
                $scope.departmentSelect =eval("("+res.data+")");
                //先判断 是否 有 参数值 带过来
            })
        }
    })

    $scope.changeDept = function(a,b,c){
        CertusFormAjax.ajax("deptList", {companyCode:a}, function (res) {
            $scope.departmentSelect =eval("("+res.data+")");
            //先判断 是否 有 参数值 带过来

        })
    }

    $scope.deptChange = function(a){
        for(var i in this.departmentSelect){
            if(this.departmentSelect[i].departmentId == a){
                $scope.dept.superiorDepartname = this.departmentSelect[i].department;
            }
        }
    }

    $scope.submits = function(){
        CertusJsonAjax.ajax("addDept",$scope.dept, function (res) {
            history.back();
        })
    }

    $scope.relatedUser = function(){
        $scope.submitted = false;
        var regex = /[0-9]*/g;
        var numList =  $scope.dept.principalName.match(regex);
        CertusFormAjax.ajax("confirmUserId",{name: numList.join().replace(",","")},function(res){
            if(res.data != "fail"){
                $scope.dept.principalId = res.data;
                $scope.submitted = true;
            }
        })

        if($scope.dept.principalName != ""){
            CertusFormAjax.ajax("relatedUser",{name:$scope.dept.principalName}, function (res) {
                $scope.userSelect = res.data;
            });
        }
    }



}